package com.common.service;


import com.common.dao.BaseDAO;
import com.common.dao.UserDAO;
import com.common.entity.User;
import com.common.tools.query.QueryDTO;
import com.common.tools.query.SpecificationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @classDescription:用户管理Service
 * @author:xiayingjie
 * @createTime:15-5-21 17:25
 */
@Service
@Transactional
public class UserService extends BaseService<User,Integer> {
    @Autowired
    private UserDAO userDAO;




    /**
     * 根据用户名密码进行登陆
     *
     * @param userName
     * @param password
     * @return
     */
    public User login(String userName, String password) {
        User u = this.userDAO.findByLoginName(userName);
        if (null != u) {
            if (u.getPassword().equals(password)) {
                return u;
            }
        }
        return null;
    }

    /**
     * 根据用户名密码进行登陆
     *
     * @return
     */
    @Cacheable("simpleCache" )
    public User findUser(int id) {
        User u = this.userDAO.findOne(id);
        return u;
    }

    //@CachePut 修改或新增的时候使用
    //condition 满足缓存条件的数据才会放入缓存，condition在调用方法之前和之后都会判断
    //unless 用于否决缓存更新的，不像condition，该表达只在方法执行之后判断，此时可以拿到返回值result进行判断了
    //结果对象 使用 #result.
//    @Cacheable(value = "user", key = "#id", condition = "#id lt 10") id 小于10则查询缓存 方法执行之前
//    @CachePut(value = "user", key = "#id", condition = "#result.username ne 'zhang'") 返回结果 username ==zhang 为true则放入缓存
//    @CachePut(value = "user", key = "#user.id", unless = "#result.username eq 'zhang'") 返回结果 username==zhang 为false 则放入缓存
    @CacheEvict(value = "simpleCache",key="#a0.id")///allEntries = true 是否移除所有数据 beforeInvocation=false 表示方法执行之后调用
    public void deleteUser(User user) {
       this.userDAO.delete(user);
    }

    //caching使用多个key

//    @Caching(
//            put = {
//                    @CachePut(value = "user", key = "#user.id"),
//                    @CachePut(value = "user", key = "#user.username"),
//                    @CachePut(value = "user", key = "#user.email")
//            }
//    )
//    @Target({ElementType.METHOD, ElementType.TYPE})
//    @Retention(RetentionPolicy.RUNTIME)
//    @Inherited
//    public @interface UserSaveCache {
//    }

//    @UserSaveCache
//    public User save(User user)

//查询
//    @Caching(
//            cacheable = {
//                    @Cacheable(value = "user", key = "#username")
//            }
//    )
//    public User findByUsername(final String username)

    /**
     * 根据用户名查询
     * @param queryDTO
     * @param loginName
     * @return
     */
    public Page<User> findList(QueryDTO queryDTO,String loginName) {
        Map searchMap=new HashMap();
        searchMap.put("search_loginName__LIKE_S", String.valueOf(loginName));
        queryDTO.setPage(queryDTO.getPage() - 1);
        PageRequest pageRequest= SpecificationUtil.getPageRequest(queryDTO);
        Specification specification = null;
        specification= SpecificationUtil.bySearchFilter(searchMap, User.class);
        Page<User> page=this.userDAO.findAll(specification, pageRequest);

        return page;
    }

    /**
     * 检查名称是否存在
     * @param name 0表示LoginName,1表示NickName
     * @param type
     * @return
     */
    public boolean checkName(String name ,int type){
        boolean flag=false;
        User u;
        if(0==type){
            u=this.userDAO.findByLoginName(name);
        }else{
            u=this.userDAO.findByNickName(name);
        }
        if(null!=u){
            flag=true;
        }
        return flag;
    }

    /**
     * 修改用户信息
     * @return
     */
    public void alterUser(User user){
        this.userDAO.save(user);
    }

    /**
     * 检查令牌
     * @return
     */
    public User checkAccessTocken(String accessToken){
       return this.userDAO.findByAccessToken(accessToken);
    }

    /**
     * 根据登录名或者nickName查询名称
     * @param name
     * @param type
     * @return
     */
    public boolean fllowUser(String name ,int type){
        boolean flag=false;
        User u;
        if(0==type){
            u=this.userDAO.findByLoginName(name);
        }else{
            u=this.userDAO.findByNickName(name);
        }
        if(null!=u){
            flag=true;
        }
        return flag;
    }


    /**
     * 根据登录名查询对象
      * @param loginName
     * @return
     */
    public User findUserByLoginName(String loginName){
        return this.userDAO.findByLoginName(loginName);
    }



    @Override
    public BaseDAO getDAO() {
        return userDAO;
    }


}
